//
//  UserSetttingController.m
//  Fucker
//
//  Created by Coby on 17/9/12.
//  Copyright © 2017年 梦拓科技. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "UserSettingController.h"
#import "TaskWallSystem.h"
#import "ViewController.h"
@interface UserSettingController()
@property (nonatomic, weak) IBOutlet UITextField * userIdTField;
@property (nonatomic, weak) IBOutlet UITextField * deviceID_Num;
@property (nonatomic, strong) NSString * userID;
@property (nonatomic, strong) NSString * serviceid;

@end

@implementation UserSettingController

- (void)viewDidLoad {
    NSLog(@"viewDidLoadCommitTaskController");
    [super viewDidLoad];
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];

}

-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    [self.userIdTField resignFirstResponder];
    [self.deviceID_Num resignFirstResponder];
}

-(void)viewWillAppear:(BOOL)animated{
    [self LoadTextFromSettingFile];
    NSLog(@"viewDidDisappearCOmmitTaskController");
    
    [super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
    NSLog(@"viewWillDisappearCommitTaskController");
    [self saveSettingText];
    [super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated{
    NSLog(@"viewDidDisappearCOmmitTaskController");
    [super viewDidDisappear:animated];
    
}
-(void)LoadTextFromSettingFile{
    NSFileManager * fileManger = [NSFileManager defaultManager];
    if([fileManger fileExistsAtPath:UserSettingFile isDirectory:false]==true){
        NSMutableDictionary * tempMutableDictionary=[NSMutableDictionary dictionaryWithContentsOfFile:UserSettingFile];
        if (tempMutableDictionary[@"userID"]){
            _userID=tempMutableDictionary[@"userID"];
            _userIdTField.text = _userID;
            _deviceID_Num.text = tempMutableDictionary[@"device"];
        }
      
    }
}
-(void)ParentLogStr:(NSString * )str{
    [(ViewController *)[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] LogStr:str];
}
-(void)saveSettingText{
    NSFileManager * fileManger = [NSFileManager defaultManager];
    NSMutableDictionary * tempInputTextDictionary=[[NSMutableDictionary alloc] init];
    tempInputTextDictionary[@"userID"]=_userIdTField.text;
    tempInputTextDictionary[@"device"]=_deviceID_Num.text;
    NSLog(@"保存用户设置参数%@",tempInputTextDictionary);
    if([fileManger fileExistsAtPath:UserSettingFile isDirectory:false]==true){
        [fileManger removeItemAtPath:UserSettingFile error:nil];
    }
    [[TaskWallSystem sharedInstance] setUserId:_userIdTField.text];
    [tempInputTextDictionary writeToFile:UserSettingFile atomically:NO];
    
//    NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithContentsOfFile:UserSettingFile];
    
}

- (IBAction)getDeviceIDWasClicked:(id)sender {
    
    NSLog(@"获取设备编号");
    
    
    NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithContentsOfFile:UserDeviceIDFile];
    if (muDict == nil) {
//        _deviceId = muDict[@"deviceId"];
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"未查询到deviceId，请先运行脚本！" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:cancelAction];
        [self presentViewController:alertControl animated:YES completion:nil];
        return;
    }
    
    [(ViewController *)[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] LogStr:[NSString stringWithFormat:@"更新设备编号：%@",muDict[@"deviceId"]]];
    
    if ([self.deviceID_Num.text isEqualToString:@""] || self.deviceID_Num.text == nil || self.deviceID_Num.text.length == 0) {
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入设备编号" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:cancelAction];
        [self presentViewController:alertControl animated:YES completion:nil];
    }else if ([[TaskWallSystem sharedInstance] deviceId] == nil){
        
        UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"提示" message:@"未查询到deviceId，请先运行脚本！" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertControl addAction:cancelAction];
        [self presentViewController:alertControl animated:YES completion:nil];
    }else {
        
        NSDictionary *param = @{
                                @"deviceId":[[TaskWallSystem sharedInstance] deviceId],
                                @"deviceString":[[TaskWallSystem sharedInstance] deviceString],
                                @"systemVersion":[[TaskWallSystem sharedInstance] systemVersion],
                                @"appVersion":[[TaskWallSystem sharedInstance] appVersion],
                                @"deviceName":self.deviceID_Num.text,
                                @"ssid":@"ssid",
                                @"bssid":@"bssid"
                                };
        __weak UserSettingController *weakSelf = self;
        [[TaskWallSystem sharedInstance] baseRequestWithParam:param requestURL:@"/deviceInfoUpdate" succeededBlock:^(id responseObject) {
            
            __weak UserSettingController *strongSelf = weakSelf;
            [(ViewController *)[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] LogStr:[NSString stringWithFormat:@"更新设备编号，code：%@",responseObject[@"code"]]];
            [(ViewController *)[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] LogStr:[NSString stringWithFormat:@"更新设备编号，message：%@",responseObject[@"message"]]];
            
            [strongSelf.navigationController popViewControllerAnimated:YES];
            
        } errorBlock:^(NSError *error) {
            
            [(ViewController *)[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] LogStr:[NSString stringWithFormat:@"更新设备编号错误！"]];
        }];
    }
    
}

//onlineTime,总的在线时间,单位s
//gameArea,游戏所在的服务区,服务器选区的就默认为0
//level,游戏等级
//manulArea 手工输入的区服
//manuLevelStart 开始手工输入的等级
//manuLevelEnd 结束手工输入的等级
@end
